

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;

namespace JTC.MiniCommerce.Web.admin {
  public partial class userlist : System.Web.UI.Page {

    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        SetUserListProperties();
        if (!Page.IsPostBack) {
          BindMembershipUserCollection();
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(userlist).Name + ".Page_Load", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the btnSearch control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnSearch_Click(object sender, EventArgs e) {
      try {
        if (!string.IsNullOrEmpty(txtSearchBy.Text.Trim())) {
          MembershipUserCollection membershipUserCollection = null;
          string text = txtSearchBy.Text.Trim();
          text = text.Replace("*", "%");
          text = text.Replace("?", "_");
          if (ddlSearchBy.SelectedIndex == 0 /* userID */) {
            membershipUserCollection = Membership.FindUsersByName(text);
          }
          else {
            membershipUserCollection = Membership.FindUsersByEmail(text);
          }
          gvUserList.PageSize = 1000; //set this number high so as to display all results
          gvUserList.PageIndex = 0;
          gvUserList.DataSource = membershipUserCollection;
          gvUserList.RowDataBound += new GridViewRowEventHandler(gvUserList_RowDataBound);
          gvUserList.DataBind();
          lblNumberOfTotalUsers.Text = membershipUserCollection.Count.ToString();
          SetUserListHeaderLabels();
          hlShowAll.Visible = true;
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(userlist).Name + ".btnSearch_Click", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Enableds the changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    public void EnabledChanged(object sender, EventArgs e) {
      try {
        string userID = null;
        CheckBox checkBox = sender as CheckBox;
        if (checkBox == null)
          return;

        if (!string.IsNullOrEmpty(checkBox.Attributes["Value"]))
          userID = checkBox.Attributes["Value"];

        if (userID == null)
          return;

        MembershipUser user = Membership.FindUsersByName(userID)[userID];
        user.IsApproved = checkBox.Checked;

        Membership.UpdateUser(user);
      }
      catch (Exception ex) {
        Logger.Error(typeof(userlist).Name + ".EnabledChanged", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the lbDelete control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
    protected void lbDelete_Click(object sender, CommandEventArgs e) {
      try {
        if (e.CommandName.Equals("mydelete")) {
          string userName = (string)e.CommandArgument;
          Membership.DeleteUser(userName);
          Response.Redirect("~/admin/userlist.aspx", false);
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(userlist).Name + ".lbDelete_Click", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the PageIndexChanging event of the gvUserList control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
    protected void gvUserList_PageIndexChanging(object sender, GridViewPageEventArgs e) {
      BindMembershipUserCollection();
      gvUserList.PageIndex = e.NewPageIndex;
      gvUserList.DataBind();
      SetUserListHeaderLabels();
    }

    /// <summary>
    /// Handles the RowDataBound event of the gvUserList control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
    void gvUserList_RowDataBound(object sender, GridViewRowEventArgs e) {
      if (e.Row.RowType == DataControlRowType.DataRow) {
        LinkButton lbDelete = e.Row.Cells[5].FindControl("lbDelete") as LinkButton;
        if (lbDelete != null) {
          lbDelete.Text = LocalizationUtility.GetText("lblDelete");
        }
      }
    }

    #endregion

    #region Methods

    #region Protected

    /// <summary>
    /// Formats the edit URL.
    /// </summary>
    /// <param name="userName">Name of the user.</param>
    /// <returns></returns>
    protected string FormatEditUrl(string userName) {
      return string.Format("useredit.aspx?userName={0}", userName);
    }

    #endregion

    #region Private

    /// <summary>
    /// Sets the user list properties.
    /// </summary>
    private void SetUserListProperties() {
      this.Title = LocalizationUtility.GetText("titleUserAdministration");
      LocalizationUtility.AddLabelText(lblUserAdministration);
      LocalizationUtility.AddPanelText(pnlUserSearch);
      LocalizationUtility.AddPanelText(pnlUserList);
      LocalizationUtility.AddLabelText(lblSearchBy);
      LocalizationUtility.AddButtonText(btnSearch);
      LocalizationUtility.AddHyperLinkText(hlShowAll);
      LocalizationUtility.AddHoverHelp(hlAddUser);
      LocalizationUtility.AddLabelText(lblTotalUsers);
      if (!Page.IsPostBack) {
        ddlSearchBy.Items.Add(new ListItem(LocalizationUtility.GetText("lblUserName")));
        ddlSearchBy.Items.Add(new ListItem(LocalizationUtility.GetText("lblEmailAddress")));
      }
    }

    /// <summary>
    /// Binds the membership user collection.
    /// </summary>
    private void BindMembershipUserCollection() {
      MembershipUserCollection membershipUserCollection = Membership.GetAllUsers();
      gvUserList.DataSource = membershipUserCollection;
      gvUserList.RowDataBound += new GridViewRowEventHandler(gvUserList_RowDataBound);
      SetUserListHeaderLabels();
      gvUserList.DataBind();
      lblNumberOfTotalUsers.Text = membershipUserCollection.Count.ToString();
    }

    /// <summary>
    /// Sets the user list header labels.
    /// </summary>
    private void SetUserListHeaderLabels() {
      gvUserList.Columns[0].HeaderText = LocalizationUtility.GetText("hdrActive");
      gvUserList.Columns[1].HeaderText = LocalizationUtility.GetText("hdrUserName");
      gvUserList.Columns[2].HeaderText = LocalizationUtility.GetText("hdrEmailAddress");
      gvUserList.Columns[3].HeaderText = LocalizationUtility.GetText("hdrCreatedOn");
      gvUserList.Columns[4].HeaderText = LocalizationUtility.GetText("hdrLastLogin");
      gvUserList.Columns[5].HeaderText = LocalizationUtility.GetText("hdrDelete");
    }

    #endregion

    #endregion

  }
}
